package com.cskaoyan.controller.admin.promotecontroller;


import com.cskaoyan.bo.promoteBO.CouponCreateBO;
import com.cskaoyan.bo.promoteBO.CouponUpdateBO;
import com.cskaoyan.service.promoteservice.CouponService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.promotevo.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @Author: feisl
 * @Date: 2021/5/10 1:19
 */
@RestController
@RequestMapping("admin/coupon")
public class CouponController {

    @Autowired
    CouponService couponService;
    @RequiresPermissions("admin:coupon:list")
    @RequestMapping("list")
    public BaseRespVo list(Integer page, Integer limit, String name,Short type,Short status,String sort, String order)
    {
        CouponListVO couponListVO = couponService.queryCouponAll(page,limit,name,type,status,sort,order);

        return BaseRespVo.ok(couponListVO);

    }
    @RequiresPermissions("admin:coupon:create")
    @RequestMapping("create")
    public BaseRespVo insert(@RequestBody CouponCreateBO couponCreateBO) throws ParseException {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(couponCreateBO.getStartTime() != null){
            Date start = simpleDateFormat.parse(couponCreateBO.getStartTime());
            Date end = simpleDateFormat.parse(couponCreateBO.getEndTime());
            if(end.compareTo(start)<0){
                return BaseRespVo.fail402("时间设置错误");
            }
        }

        CouponCreateVO couponCreateVO = couponService.insert(couponCreateBO);

        return BaseRespVo.ok(couponCreateVO);
    }
    @RequiresPermissions("admin:coupon:update")
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody CouponUpdateBO couponBO) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(couponBO.getStartTime() != null || couponBO.getEndTime() != null){
            Date start = simpleDateFormat.parse(couponBO.getStartTime());
            Date end = simpleDateFormat.parse(couponBO.getEndTime());
            if(end.compareTo(start)<0){
                return BaseRespVo.fail402("时间设置错误");
            }
        }

        CouponUpdateVO couponUpdateVO = couponService.update(couponBO);

        return  BaseRespVo.ok(couponUpdateVO);
    }
    @RequiresPermissions("admin:coupon:read")
    @RequestMapping("read")
    public BaseRespVo read(Integer id){

        CouponReadVO couponReadVO = couponService.read(id);

        return BaseRespVo.ok(couponReadVO);
    }

    //实例中没有数据，没有实现
    @RequiresPermissions("admin:coupon:listuser")
    @RequestMapping("listuser")
    public BaseRespVo listuser(Integer page, Integer limit,Integer couponId,Integer userId,Short status,String sort,String order){

        ListuserVO listuserVO = couponService.listuser(page,limit,couponId,userId,status,sort,order);

        return BaseRespVo.ok(listuserVO);
    }
    @RequiresPermissions("admin:coupon:delete")
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map map){

        Object id = map.get("id");

        couponService.delete((Integer) id);

        return BaseRespVo.ok();
    }
}
